import json
import unittest
from api.login import LoginApi
from parameterized import parameterized


def build_data():
    file = 'D:\内网通储存\刘禹辰\IhrmApi\data/login_data.json'
    test_data = []
    with open(file, 'r', encoding='utf-8') as f:
        json_data = json.load(f)
        for case_data in json_data:
            login_data = case_data.get('login_data')
            status_code = case_data.get('status_code')
            success = case_data.get('success')
            code = case_data.get('code')
            message = case_data.get('message')
            test_data.append((login_data, status_code, success, code, message))
    return test_data


class TestLogin(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.login_api = LoginApi()

    @parameterized.expand(build_data)
    def test01_case001(self, login_data, status_code, success, code, message):
        response = self.login_api.login(login_data)
        json_data = response.json()
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(success, json_data.get('success'))
        self.assertEqual(code, json_data.get('code'))
        self.assertIn(message, json_data.get('message'))


    # def test01_case002(self):
    #     response = self.login_api.login({
    #         'mobile': '13800000000',
    #         'password': '123456'
    #     })
    #     json_data = response.json()
    #     self.assertEqual(200, response.status_code)
    #     self.assertEqual(False, json_data.get('success'))
    #     self.assertEqual(20001, json_data.get('code'))
    #     self.assertIn('用户名或密码错误', json_data.get('message'))
    #
    # def test01_case003(self):
    #     response = self.login_api.login(None)
    #     json_data = response.json()
    #     self.assertEqual(200, response.status_code)
    #     self.assertEqual(False, json_data.get('success'))
    #     self.assertEqual(99999, json_data.get('code'))
    #     self.assertIn('抱歉，系统繁忙，请稍后重试！', json_data.get('message'))
